<script setup lang="ts">
import { useUserStore } from '@/store/modules/user';
import { to } from 'await-to-js';
import type { LoginData } from '@/api/types';

const router = useRouter();
const userStore = useUserStore();

const handleLogin = async () => {
    const loading = ElLoading.service({
        lock: true,
        text: '登录中...',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    const searchParams = new URLSearchParams(location.search)
    // 调用action的登录方法
    const submitData = {
        grantType: 'weixinpub',
        id: searchParams.get('id'),
        wxcode: searchParams.get('code'),
        code: searchParams.get('ncode'),
        sydId: searchParams.get('sydId'),
        domain: location.origin,
    }
    const [err] = await to(userStore.login(submitData as unknown as LoginData));
    setTimeout(() => {
        loading.close()
    }, 5000);
    if (!err) {
        await router.push({ path: '/' });
    }
};

onMounted(() => {
    handleLogin()
});
</script>
